﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FantasyBlog.DAL;
using FantasyBlog.BLL;

namespace FantasyBlog.Controllers
{ 
    public class PostController : Controller
    {
       // private BlogModelContainer db = new BlogModelContainer();

        //
        // GET: /Post/

        public ViewResult Index(int page=1)
        {

            var posts = provider.GetPosts().FilterOpenedPosts().TakeSkip(page, take);
            int total = posts.Count();
            ViewBag.take = take;
            ViewBag.total = (total - 1) / take + 1;
            ViewBag.currentPage = 1;
            if (total == 0)
                return View("NotFound");
            return View(posts);
        }
        //
        // GET: /Post/Details/5

        public ViewResult Details(int id)
        {
            Post post = provider.GetPost(id);
            if (post == null)
                return View("NotFound");
            return View(post);
        }

        public ViewResult Search(string keyword,int page=1)
        {
            
            ViewBag.info = keyword + " 的搜索结果";
            var posts = provider.SearhPost(keyword).FilterOpenedPosts().TakeSkip(page, take);
            int total = posts.Count();
            ViewBag.total = (total - 1) / take + 1;
            ViewBag.currentPage = page;
            if (total == 0)
                return View("NotFound");
            return View("Index",posts);
        }

        public ViewResult Tag(string id,int page=1)
        {
            
            ViewBag.info = "标签归档："+id;
            var posts = provider.GetTagPosts(id).FilterOpenedPosts().TakeSkip(page, take);
            int total = posts.Count();
            ViewBag.total = (total - 1) / take + 1;
            ViewBag.currentPage = page;
            if (total == 0)
                return View("NotFound");
            return View("Index", posts);
        }

        public ViewResult Author(string id, int page=1)
        {
            ViewBag.info = "作者归档：" + id;
            var posts = provider.GetPostByUser(id).FilterOpenedPosts().TakeSkip(page,take);
            int total = posts.Count();
            ViewBag.total = (total-1)/take +1;
            ViewBag.take = take;
            ViewBag.currentPage = page;
            if (total == 0)
                return View("NotFound");
            return View("Index",posts);
        }
       
        public ViewResult Category(string id, int page = 1)
        {
            ViewBag.info = "分类归档：" + id;
            int total = 0;
            var posts = provider.GetPostsByCategory(id).TakeSkip(page, take);
            total = posts.Count();
            ViewBag.total = (total - 1) / take + 1;
            ViewBag.currentPage = page;
            ViewBag.take = take;
            if (total == 0)
                return View("NotFound");
            return View("Index", posts);
        }

        [HttpPost]
        public ActionResult Comment(CommentModel model)
        {
            if (ModelState.IsValid)
            {
                var c = new Comment();
                if (User.Identity.IsAuthenticated)
                {
                    var user = (from u in Model.UserSet
                                where u.username == User.Identity.Name
                                select u).Single();
                    c.author = user.username;
                    c.author_email = user.email;
                    c.User = user;
                }
                else
                {
                    if (string.IsNullOrEmpty(model.author_email) || string.IsNullOrEmpty(model.author))
                        return View("Error");
                    c.author = model.author;
                    c.author_email = model.author_email;
                }
                var post = Model.PostSet.Find(model.postid);
                if (post == null)
                    return View("Error");
                c.Post = post;
                if (model.parent != 0)
                {
                    var pc = Model.CommentSet.Find(model.parent);
                    if (pc == null || pc.Post.ID != post.ID)
                    {
                        return View("Error");
                    }
                    c.Parent = pc;
                }
                if(string.IsNullOrEmpty(model.comment))
                    return View("Error");
                c.content = model.comment;
                c.date = DateTime.Now;
                c.ip = "123";
                c.comment_approved = "1";
                Model.CommentSet.Add(c);
                Model.SaveChanges();
            }
            return Redirect("/Post/Details/" + model.postid.ToString());
            
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private int take = 10;
        private IPostProvider provider = ProviderAFactory.OpenPostProvider;
        private BlogModelContainer Model = new BlogModelContainer();
    }
}